package org.jeecg.modules.lab.items.temp2.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Norm;
import org.jeecg.modules.lab.items.temp2.service.ILabItemsTemp2NormService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.lab.items.vo.LabItemsTargetPage;
import org.jeecg.modules.lab.items.vo.LabItemsTargetVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 实验项目模板_检测条件
 * @Author: jeecg-boot
 * @Date:   2023-11-17
 * @Version: V1.0
 */
@Api(tags="实验项目模板_检测条件")
@RestController
@RequestMapping("/items/labItemsTemp2Norm")
@Slf4j
public class LabItemsTemp2NormController extends JeecgController<LabItemsTemp2Norm, ILabItemsTemp2NormService> {
	@Autowired
	private ILabItemsTemp2NormService labItemsTemp2CondService;
	@Autowired
	private ILabCronJobService labCronJobService;
	
	/**
	 * 分页列表查询
	 *
	 * @param labItemsTemp2Cond
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "实验项目模板_检测条件-分页列表查询")
	@ApiOperation(value="实验项目模板_检测条件-分页列表查询", notes="实验项目模板_检测条件-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabItemsTemp2Norm>> queryPageList(LabItemsTemp2Norm labItemsTemp2Cond,
														  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														  HttpServletRequest req) {
		QueryWrapper<LabItemsTemp2Norm> queryWrapper = QueryGenerator.initQueryWrapper(labItemsTemp2Cond, req.getParameterMap());
		Page<LabItemsTemp2Norm> page = new Page<LabItemsTemp2Norm>(pageNo, pageSize);
		IPage<LabItemsTemp2Norm> pageList = labItemsTemp2CondService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param labItemsTemp2Cond
	 * @return
	 */
	@AutoLog(value = "实验项目模板_检测条件-添加")
	@ApiOperation(value="实验项目模板_检测条件-添加", notes="实验项目模板_检测条件-添加")
//	@RequiresPermissions("cond:lab_items_temp2_cond:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabItemsTemp2Norm labItemsTemp2Cond) {
		labItemsTemp2CondService.save(labItemsTemp2Cond);

		if (MyEnum.sync){
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_items_temp2_norm","0",time,1);
			labCronJobService.save(job);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param labItemsTemp2Cond
	 * @return
	 */
	@AutoLog(value = "实验项目模板_检测条件-编辑")
	@ApiOperation(value="实验项目模板_检测条件-编辑", notes="实验项目模板_检测条件-编辑")
//	@RequiresPermissions("cond:lab_items_temp2_cond:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabItemsTemp2Norm labItemsTemp2Cond) {
		labItemsTemp2Cond.setSynFlag("1");
		labItemsTemp2CondService.updateById(labItemsTemp2Cond);

		if (MyEnum.sync){
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_items_temp2_norm","1",time,1);
			labCronJobService.save(job);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实验项目模板_检测条件-通过id删除")
	@ApiOperation(value="实验项目模板_检测条件-通过id删除", notes="实验项目模板_检测条件-通过id删除")
//	@RequiresPermissions("cond:lab_items_temp2_cond:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labItemsTemp2CondService.removeById(id);

		if (MyEnum.sync){
			long time = new Date().getTime();
			String condition="id='"+id+"'";
			LabCronJob job = new LabCronJob("lab_items_temp2_norm","2",condition,time,1);
			labCronJobService.save(job);
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "实验项目模板_检测条件-批量删除")
	@ApiOperation(value="实验项目模板_检测条件-批量删除", notes="实验项目模板_检测条件-批量删除")
//	@RequiresPermissions("cond:lab_items_temp2_cond:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labItemsTemp2CondService.removeByIds(Arrays.asList(ids.split(",")));

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String[] split = ids.split(",");
			labCronJobService.saveCronBatch("lab_items_temp2_norm","2",time,split);
		}
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "实验项目模板_检测条件-通过id查询")
	@ApiOperation(value="实验项目模板_检测条件-通过id查询", notes="实验项目模板_检测条件-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabItemsTemp2Norm> queryById(@RequestParam(name="id",required=true) String id) {
		LabItemsTemp2Norm labItemsTemp2Cond = labItemsTemp2CondService.getById(id);
		if(labItemsTemp2Cond==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labItemsTemp2Cond);
	}

	 /**
	  * 添加条件
	  *
	  * @param labItemsTargetPage
	  * @return
	  */
	 @AutoLog(value = "实验项目模板-添加条件")
	 @ApiOperation(value = "实验项目模板-添加条件", notes = "实验项目模板-添加条件")
	 @PostMapping(value = "/addNorm")
	 public Result<String> addNorm(@RequestBody LabItemsTargetPage labItemsTargetPage) {
		 String itemsId = labItemsTargetPage.getItemsId();
		 //查询已有指标
		 QueryWrapper<LabItemsTemp2Norm> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("items_id", itemsId);
		 List<LabItemsTemp2Norm> labItemsTemp2NormList = labItemsTemp2CondService.list(queryWrapper);
		 int num = labItemsTemp2NormList.size();
		 List<LabItemsTargetVo> itemsTargetList = labItemsTargetPage.getItemsTargetList();
		 if (num > 0) {
			 for (int i = 0; i < itemsTargetList.size(); i++) {
				 LabItemsTargetVo targetVo = itemsTargetList.get(i);
				 boolean bool = true;
				 for (LabItemsTemp2Norm norm : labItemsTemp2NormList) {
					 if (targetVo.getFieldName().equals(norm.getFieldName())) {
						 bool = false;
						 break;
					 }
				 }
				 if (bool) {
					 LabItemsTemp2Norm norm = new LabItemsTemp2Norm();
					 norm.setItemsId(itemsId);
					 norm.setFieldName(targetVo.getFieldName());
					 norm.setFieldCode(targetVo.getFieldCode());
					 norm.setFieldType(targetVo.getFieldType());
					 norm.setFieldSort(num + i + 1);
					 labItemsTemp2CondService.save(norm);
				 }
			 }
		 } else {
			 for (int i = 0; i < itemsTargetList.size(); i++) {
				 LabItemsTargetVo targetVo = itemsTargetList.get(i);
				 LabItemsTemp2Norm norm = new LabItemsTemp2Norm();
				 norm.setItemsId(itemsId);
				 norm.setFieldName(targetVo.getFieldName());
				 norm.setFieldCode(targetVo.getFieldCode());
				 norm.setFieldType(targetVo.getFieldType());
				 norm.setFieldSort(i + 1);
				 labItemsTemp2CondService.save(norm);
			 }
		 }

		 if (MyEnum.sync){
			 long time = new Date().getTime();
			 LabCronJob job = new LabCronJob("lab_items_temp2_norm","0",time,1);
			 labCronJobService.save(job);
		 }

		 return Result.OK("条件添加成功！");
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param labItemsTemp2Cond
    */
    @RequiresPermissions("cond:lab_items_temp2_cond:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabItemsTemp2Norm labItemsTemp2Cond) {
        return super.exportXls(request, labItemsTemp2Cond, LabItemsTemp2Norm.class, "实验项目模板_检测条件");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("cond:lab_items_temp2_cond:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabItemsTemp2Norm.class);
    }

}
